package com.ysstech.kettle.controller;


import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JdbcUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobDatasource;
import com.ysstech.kettle.enums.KettleDbDriverEnum;
import com.ysstech.kettle.enums.KettleDbUrlEnum;
import com.ysstech.kettle.enums.KettleEnum;
import com.ysstech.kettle.service.JobDatasourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@Controller
@Slf4j
@Api(tags = "资源库管理api")
@RequestMapping("/jobDatasource")
public class JobDatasourceController extends BaseController {

    @Autowired
    private JobDatasourceService jobDatasourceService;


    @YssPermissions(value = {"kettle:job:datasource"})
    @ApiOperation(value = "跳转资源库管理管理页面")
    @YssLog(module = "资源库管理管理", remark = "跳转资源库管理管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "kettle/jobDatasource";
    }

    @YssPermissions(value = {"kettle:job:datasource"})
    @ApiOperation(value = "获取资源库管理页面初始值")
    @YssLog(module = "资源库管理管理", remark = "获取资源库管理管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //数据库类型
        List<Dicvalue> list = dicValueService.getCacheDicValueList(KettleEnum.KETTLE_DATABASE_TYPE.getCode());
        map.put("ftype", list);
        map.put("ftype2", list);
        map.put("dbDriver", KettleDbDriverEnum.getMapEnum());
        map.put("dbUrl", KettleDbUrlEnum.getMapEnum());
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取资源库管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:datasource"})
    @ApiOperation(value = "获取资源库管理信息")
    @YssLog(module = "资源库管理管理", remark = "获取资源库管理信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(JobDatasource jobDatasource, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取资源库管理信息成功",
                jobDatasourceService.queryAllDatasource(jobDatasource, this.getPage(page, rows)));
    }


    /**
     * 新增资源库管理的方法
     *
     * @param jobDatasource
     * @return
     */
    @YssPermissions(value = {"kettle:job:datasource:add"})
    @ApiOperation(value = "新增资源库管理信息")
    @YssLog(module = "资源库管理管理", remark = "新增资源库管理信息")
    @RequestMapping(value = "/addJobDatasource")
    @ResponseBody
    public RestResult addJobDatasource(JobDatasource jobDatasource) throws Exception {
        if ("FILE".equals(jobDatasource.getFtype())) { //文件类型不测试
            jobDatasource.setId(DateUtil.generateUUID());
            jobDatasource.setAddTime(LocalDateTime.now());
            jobDatasourceService.save(jobDatasource);
            return ResultGenerator.getSuccessResult("新增资源库管理信息成功！");
        }
        Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                jobDatasource.getUsername(), jobDatasource.getPassword());
        if (falg) {
            jobDatasource.setId(DateUtil.generateUUID());
            jobDatasource.setAddTime(LocalDateTime.now());
            jobDatasourceService.save(jobDatasource);
            return ResultGenerator.getSuccessResult("新增资源库管理信息成功！", falg);
        } else {
            return ResultGenerator.getSuccessResult("获取数据连接失败！", falg);
        }
    }

    /**
     * 修改资源库管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:datasource:edit"})
    @ApiOperation(value = "修改资源库管理信息")
    @YssLog(module = "资源库管理管理", remark = "修改资源库管理信息")
    @RequestMapping(value = "/editJobDatasource")
    @ResponseBody
    public RestResult editJobDatasource(JobDatasource jobDatasource) throws Exception {
        if (jobDatasource == null || StringUtils.isBlank(jobDatasource.getId())) {
            throw new BusinessException("获取资源库管理信息失败，请稍后重试！");
        }
        if ("FILE".equals(jobDatasource.getFtype())) { //文件类型不测试
            jobDatasource.setEditTime(LocalDateTime.now());
            jobDatasourceService.updateById(jobDatasource);
            return ResultGenerator.getSuccessResult("修改资源库管理信息成功！");
        }
        Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                jobDatasource.getUsername(), jobDatasource.getPassword());
        if (falg) {
            jobDatasource.setEditTime(LocalDateTime.now());
            jobDatasourceService.updateById(jobDatasource);
            return ResultGenerator.getSuccessResult("修改资源库管理信息成功！", falg);
        } else {
            return ResultGenerator.getSuccessResult("获取数据连接失败！", falg);
        }
    }

    /**
     * 删除资源库管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:datasource:del"})
    @ApiOperation(value = "删除资源库管理信息")
    @YssLog(module = "资源库管理管理", remark = "删除资源库管理信息")
    @RequestMapping(value = "/delJobDatasource")
    @ResponseBody
    public RestResult delJobDatasource(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除资源库管理信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        jobDatasourceService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除资源库管理信息成功！");
    }


    /**
     * 修改资源库管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:datasource"})
    @ApiOperation(value = "修改资源库管理信息")
    @YssLog(module = "资源库管理管理", remark = "修改资源库管理信息")
    @RequestMapping(value = "/testConnection")
    @ResponseBody
    public RestResult testConnection(JobDatasource jobDatasource) throws Exception {
        if (jobDatasource == null || StringUtils.isBlank(jobDatasource.getDriverClassName())
                || StringUtils.isBlank(jobDatasource.getUrl())) {
            throw new BusinessException("获取资源库管理信息失败，请稍后重试！");
        }
        if ("FILE".equals(jobDatasource.getFtype())) { //文件类型不测试
            return ResultGenerator.getSuccessResult("测试资源库连接成功！", true);
        }
        Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                jobDatasource.getUsername(), jobDatasource.getPassword());
        if (falg) {
            return ResultGenerator.getSuccessResult("测试资源库连接成功！", falg);
        } else {
            return ResultGenerator.getSuccessResult("测试资源库连接失败！", falg);
        }
    }
}
